import 'package:aitao_flutter/common_ui/empty_view.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:aitao_flutter/utils/sp_utils.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../model/hello_img_model.dart';
import '../request/api.dart';
import '../request/requestManager.dart';
import '../res/r.dart';

// ignore: must_be_immutable
class GreetImagePage extends StatefulWidget {
  GreetImagePage({super.key, required this.greetImageCallback});
  Function(int) greetImageCallback;
  @override
  State<GreetImagePage> createState() => _GreetImagePageState();
}

class _GreetImagePageState extends State<GreetImagePage> {
  List<HelloImageModel> imageList = [];

  @override
  Widget build(BuildContext context) {
    return imageList.isEmpty
        ? EmptyView()
        : Container(
            padding: EdgeInsetsStyle.all(10),
            child: GridView.builder(
                itemCount: imageList.length,
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    crossAxisSpacing: 10,
                    mainAxisSpacing: 10),
                itemBuilder: (context, index) {
                  HelloImageModel model = imageList[index];
                  return GestureDetector(
                    child: Container(
                      color: Colors.teal,
                      child: Stack(
                        children: [
                          Positioned(
                              child: CachedNetworkImage(
                            imageUrl: SpUtil.getImageUrl(model.imgPath ?? ''),
                          )),
                          Visibility(
                            visible: model.isAudit == 0,
                            child: Positioned(
                                child: Container(
                              color: const Color.fromRGBO(29, 29, 29, 0.58),
                              child: Center(
                                child: Text(
                                  '审核中',
                                  style: Styles.whiteText_13,
                                ),
                              ),
                            )),
                          ),
                          Positioned(
                              right: 0,
                              child: Image.asset(ImagePath.assetsDeleteBtn)),
                        ],
                      ),
                    ),
                    onTap: () {
                      if (imageList[index].isAudit == 0) {
                        EasyLoading.showToast('招呼审核中...');
                        debugPrint('招呼审核中...');
                        return;
                      }
                      // 发送图片需要直接调用接口
                      widget.greetImageCallback(imageList[index].hiId ?? 0);
                    },
                  );
                }));
  }

  @override
  void initState() {
    super.initState();
    getImageListData();
  }

  void getImageListData() async {
    var result = await RequestManager().post(
      API.getSayHelloImg,
    );
    if (result != null && result['code'] == 0) {
      List tempList = result['data']['list'];
      if (tempList.isNotEmpty) {
        setState(() {
          imageList = tempList.map((e) {
            return HelloImageModel.fromJson(e);
          }).toList();
        });
      }
    }
  }
}
